#include "func.h"

//void userLogin(int sockfd){
//    char username[4096] = {0};
//    char salt[4096] = {0};
//    char *passwd;
//    char miwen[4096] = {0};
//    while(1){
//        memset(username, 0, sizeof(username));
//        printf("username:");
//        scanf("%s", username);
//        //传用户名
//        train_t train;
//        memset(&train, 0, sizeof(train));
//        train.length = strlen(username);
//        strcpy(train.data, username);
//        send(sockfd, &train, 4+train.length, MSG_WAITALL);
//
//        //接受盐值
//        int buflen = 0;
//        char buf[4096];
//        recv(sockfd, &buflen, 4, MSG_WAITALL);
//        recv(sockfd, buf, buflen, MSG_WAITALL);
//        if(buf[0] != '$'){
//            printf("please input right username!\n");
//            continue;
//        } else {
//            //输密码得到密文
//            passwd = getpass("passwd:");
//            strcpy(salt, buf);
//            strcpy(miwen, crypt(passwd, salt));
//            //传密文
//            memset(&train, 0, sizeof(train));
//            train.length = strlen(miwen);
//            strcpy(train.data, miwen);
//            send(sockfd, &train, 4+train.length, MSG_WAITALL);
//            //接受返回的验证消息
//            memset(&buf, 0, sizeof(buf));
//            recv(sockfd, &buflen, 4, MSG_WAITALL);
//            recv(sockfd, buf, buflen, MSG_WAITALL);
//            if(buf[0] == 'y'){
//                printf("welcom!\n");
//                return;
//            } else {
//                printf("wrong passwd\n");
//                continue;
//            }
//        }
//    }
//}
void userRegis_Login(int sockfd){
    char username[4096] = {0};
    char salt[4096] = {0};
    char *passwd;
    char miwen[4096] = {0};

    int selflag;//选择标志
    while(1){
        printf("\n1. Log in\n2. Register a new Account\n3. Exit\nplease input the number:");
        scanf("%d", &selflag);
        if(selflag == 1){
            send(sockfd, &selflag, 4, MSG_WAITALL);//发送代表登录的标志
            memset(username, 0, sizeof(username));
            printf("username:");
            scanf("%s", username);
            //传用户名
            train_t train;
            memset(&train, 0, sizeof(train));
            train.length = strlen(username);
            strcpy(train.data, username);
            send(sockfd, &train, 4+train.length, MSG_WAITALL);

            //接受盐值
            int buflen = 0;
            char buf[4096];
            recv(sockfd, &buflen, 4, MSG_WAITALL);
            recv(sockfd, buf, buflen, MSG_WAITALL);

            //输密码得到密文
            passwd = getpass("passwd:");
            strcpy(salt, buf);
            strcpy(miwen, crypt(passwd, salt));
            //test
            printf("生成的密文是：%s\n",miwen);
            //传密文
            memset(&train, 0, sizeof(train));
            train.length = strlen(miwen);
            strcpy(train.data, miwen);
            send(sockfd, &train, 4+train.length, MSG_WAITALL);
            //接受返回的验证消息
            memset(&buf, 0, sizeof(buf));
            recv(sockfd, &buflen, 4, MSG_WAITALL);
            recv(sockfd, buf, buflen, MSG_WAITALL);
            if(buf[0] == 'y'){
                printf("%s welcom!\n", username);
                return;
            } else {
                printf("wrong passwd\n");
                continue;
            }
        
        }else if(selflag == 2){
            send(sockfd, &selflag, 4, MSG_WAITALL);//发送代表注册的标志
            memset(username, 0, sizeof(username));
            printf("please input your new username:");
            scanf("%s", username);
            //传用户名
            train_t train;
            memset(&train, 0, sizeof(train));
            train.length = strlen(username);
            strcpy(train.data, username);
            send(sockfd, &train, 4+train.length, MSG_WAITALL);
            //接受盐值
            int buflen = 0;
            char buf[4096];
            recv(sockfd, &buflen, 4, MSG_WAITALL);
            recv(sockfd, buf, buflen, MSG_WAITALL);
            passwd = getpass("passwd:");
            strcpy(salt, buf);
            strcpy(miwen, crypt(passwd, salt));
            //传密文
            memset(&train, 0, sizeof(train));
            train.length = strlen(miwen);
            strcpy(train.data, miwen);
            send(sockfd, &train, 4+train.length, MSG_WAITALL);
            //接受返回的验证消息
            memset(&buf, 0, sizeof(buf));
            recv(sockfd, &buflen, 4, MSG_WAITALL);
            recv(sockfd, buf, buflen, MSG_WAITALL);
            if(buf[0] == 'y'){
                printf("Registration complete.\n");
                return;
            }  

        } else {
            exit(1);
        }
    }
}

 
